package com.sina.weibo.sdk.auth;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import com.huawei.appmarket.service.deamon.download.remote.DownloadAgentService;
import com.huawei.hms.support.api.entity.hwid.HwIDConstant.ReqAccessTokenParm;
import com.huawei.hwid.core.constants.HwAccountConstants;
import com.sina.weibo.sdk.WbSdk;
import com.sina.weibo.sdk.net.WeiboParameters;
import com.sina.weibo.sdk.web.WeiboSdkWebActivity;
import o.fnf;
import o.fnj;
import o.fnk;
import o.fnm;
import o.fno;
import o.fnr;
import o.fns;
import o.fnt;
import o.fnu;
import o.fob;

public class BaseSsoHandler {
    public static final String OAUTH2_BASE_URL = "https://open.weibo.cn/oauth2/authorize?";
    private static final String TAG = "BaseSsoHandler";
    protected final int SSO_TYPE_INVALID = 3;
    protected WbAuthListener authListener;
    protected Context mAuthActivity;
    protected int ssoRequestCode = -1;
    protected int ssoRequestType = 3;

    protected enum AuthType {
        ALL,
        SsoOnly,
        WebOnly
    }

    public BaseSsoHandler(Activity activity) {
        this.mAuthActivity = activity;
        fnj.ॱ().ˎ(activity, WbSdk.getAuthInfo().getAppKey());
    }

    public BaseSsoHandler(Context context) {
        this.mAuthActivity = context;
        fnj.ॱ().ˎ(context, WbSdk.getAuthInfo().getAppKey());
    }

    public void authorize(WbAuthListener wbAuthListener) {
        authorize(32973, wbAuthListener, AuthType.ALL);
    }

    public void authorizeClientSso(WbAuthListener wbAuthListener) {
        authorize(32973, wbAuthListener, AuthType.SsoOnly);
    }

    public void authorizeWeb(WbAuthListener wbAuthListener) {
        authorize(32973, wbAuthListener, AuthType.WebOnly);
    }

    private void authorize(int i, WbAuthListener wbAuthListener, AuthType authType) {
        resetIntentFillData();
        if (wbAuthListener == null) {
            throw new RuntimeException("please set auth listener");
        }
        this.authListener = wbAuthListener;
        if (authType == AuthType.WebOnly) {
            startWebAuth();
            return;
        }
        Object obj = null;
        if (authType == AuthType.SsoOnly) {
            obj = 1;
        }
        WbAppInfo ˋ = fnf.ॱ(this.mAuthActivity).ˋ();
        if (isWbAppInstalled() && ˋ != null) {
            startClientAuth(i);
        } else if (obj != null) {
            this.authListener.onFailure(new WbConnectErrorMessage());
        } else {
            startWebAuth();
        }
    }

    protected void startClientAuth(int i) {
        try {
            WbAppInfo ˋ = fnf.ॱ(this.mAuthActivity).ˋ();
            Intent intent = new Intent();
            intent.setClassName(ˋ.getPackageName(), ˋ.getAuthActivityName());
            intent.putExtras(WbSdk.getAuthInfo().getAuthBundle());
            intent.putExtra("_weibo_command_type", 3);
            intent.putExtra("_weibo_transaction", String.valueOf(System.currentTimeMillis()));
            intent.putExtra("aid", fnr.ˋ(this.mAuthActivity, WbSdk.getAuthInfo().getAppKey()));
            if (fno.ˊ(this.mAuthActivity, intent)) {
                fillExtraIntent(intent, i);
                try {
                    ((Activity) this.mAuthActivity).startActivityForResult(intent, this.ssoRequestCode);
                    return;
                } catch (Exception e) {
                    if (this.authListener != null) {
                        this.authListener.onFailure(new WbConnectErrorMessage());
                        return;
                    }
                    return;
                }
            }
            this.authListener.onFailure(new WbConnectErrorMessage("your install weibo app is counterfeit", "8001"));
        } catch (Exception e2) {
        }
    }

    protected void fillExtraIntent(Intent intent, int i) {
    }

    protected void resetIntentFillData() {
        this.ssoRequestCode = 32973;
    }

    protected void startWebAuth() {
        AuthInfo authInfo = WbSdk.getAuthInfo();
        WeiboParameters weiboParameters = new WeiboParameters(authInfo.getAppKey());
        weiboParameters.put(ReqAccessTokenParm.CLIENT_ID, authInfo.getAppKey());
        weiboParameters.put(ReqAccessTokenParm.REDIRECT_URI, authInfo.getRedirectUrl());
        weiboParameters.put("scope", authInfo.getScope());
        weiboParameters.put(ReqAccessTokenParm.RESPONSE_TYPE, "code");
        weiboParameters.put("version", "0041005000");
        weiboParameters.put("luicode", "10000360");
        Oauth2AccessToken readAccessToken = AccessTokenKeeper.readAccessToken(this.mAuthActivity);
        if (!(readAccessToken == null || TextUtils.isEmpty(readAccessToken.getToken()))) {
            weiboParameters.put("trans_token", readAccessToken.getToken());
            weiboParameters.put("trans_access_token", readAccessToken.getToken());
        }
        weiboParameters.put("lfid", "OP_" + authInfo.getAppKey());
        String ˋ = fnr.ˋ(this.mAuthActivity, authInfo.getAppKey());
        if (!TextUtils.isEmpty(ˋ)) {
            weiboParameters.put("aid", ˋ);
        }
        weiboParameters.put(DownloadAgentService.PACKAGE_NMAE_ARG, authInfo.getPackageName());
        weiboParameters.put("key_hash", authInfo.getKeyHash());
        String str = OAUTH2_BASE_URL + weiboParameters.encodeUrl();
        if (fnk.ˋ(this.mAuthActivity)) {
            String str2 = null;
            if (this.authListener != null) {
                fnu ˎ = fnu.ˎ();
                str2 = ˎ.ॱ();
                ˎ.ˎ(str2, this.authListener);
            }
            fob o_fob = new fob(authInfo, fnt.AUTH, str2, "微博登录", str, this.mAuthActivity);
            Intent intent = new Intent(this.mAuthActivity, WeiboSdkWebActivity.class);
            Bundle bundle = new Bundle();
            o_fob.ॱ(bundle);
            intent.putExtras(bundle);
            this.mAuthActivity.startActivity(intent);
            return;
        }
        fns.ॱ(this.mAuthActivity, "Error", "Application requires permission to access the Internet");
    }

    public void authorizeCallBack(int i, int i2, Intent intent) {
        if (32973 != i) {
            return;
        }
        if (i2 == -1) {
            if (fno.ॱ(this.mAuthActivity, fnf.ॱ(this.mAuthActivity).ˋ(), intent)) {
                Object ˊ = fnr.ˊ(intent.getStringExtra(HwAccountConstants.EXTRA_OPLOG_ERROR));
                Object ˊ2 = fnr.ˊ(intent.getStringExtra("error_type"));
                Object ˊ3 = fnr.ˊ(intent.getStringExtra("error_description"));
                fnm.ˊ(TAG, "error: " + ˊ + ", error_type: " + ˊ2 + ", error_description: " + ˊ3);
                if (TextUtils.isEmpty(ˊ) && TextUtils.isEmpty(ˊ2) && TextUtils.isEmpty(ˊ3)) {
                    Oauth2AccessToken parseAccessToken = Oauth2AccessToken.parseAccessToken(intent.getExtras());
                    if (parseAccessToken != null && parseAccessToken.isSessionValid()) {
                        fnm.ˊ(TAG, "Login Success! " + parseAccessToken.toString());
                        AccessTokenKeeper.writeAccessToken(this.mAuthActivity, parseAccessToken);
                        this.authListener.onSuccess(parseAccessToken);
                        return;
                    }
                    return;
                } else if ("access_denied".equals(ˊ) || "OAuthAccessDeniedException".equals(ˊ)) {
                    fnm.ˊ(TAG, "Login canceled by user.");
                    this.authListener.cancel();
                    return;
                } else {
                    fnm.ˊ(TAG, "Login failed: " + ˊ);
                    this.authListener.onFailure(new WbConnectErrorMessage(ˊ2, ˊ3));
                    return;
                }
            }
            this.authListener.onFailure(new WbConnectErrorMessage("your install weibo app is counterfeit", "8001"));
        } else if (i2 == 0) {
            this.authListener.cancel();
        }
    }

    @Deprecated
    public boolean isWbAppInstalled() {
        return WbSdk.isWbInstall(this.mAuthActivity);
    }
}
